package jee.boot.module.log.local;

import jee.boot.common.standard.AccountHandler;
import jee.boot.common.utils.DateUtils;
import jee.boot.entity.auth.Account;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * /oauth/token请求不能被interceptor拦截,所以得单独写个拦截器
 */
@Aspect
@Component
public class OauthTokenAspect {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private HttpServletRequest request; //自动注入request
    @Autowired
    private AccountHandler accountHandler;



    /**
     * 获取token的方法拦截
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(public * org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")
    public Object treeControllerTransactionProcessor(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime=System.currentTimeMillis();
        Object[] arguments = joinPoint.getArgs();
        Object object = joinPoint.proceed(arguments);
        logger.info("URI: {},耗时：{}   ", request.getRequestURI(), DateUtils.formatDateTime(System.currentTimeMillis() - startTime));
        Account operator =accountHandler.getPrincipal();
        LogUtils.saveLog(request,operator,null);
        return object;
    }
}
